package com.mengchengnet.chaperonage.ui.activity;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;

import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.PreferenceUtil;

//引导页
public class WelcomeActivity extends BaseActivity {

    public static final String TAG = "WelcomeActivity"; //活动TAG
    private String versionCode,versionName;
    private boolean isLogin;//判断用户登录状态
    private String userType;//判断当前用户是普通用户还是陪护人员

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbarState = TOOLBAR_STYLE_NONE;
        initToolbar(null);
        setContentView(R.layout.activity_welcome);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        isLogin= PreferenceUtil.getBoolean(this,"isLogin",false);//登录状态
        userType=PreferenceUtil.getString(this,"userType");//用户类型
//        if (!DeviceUtil.isNetworkAvailable()) {
//            Toast.makeText(WelcomeActivity.this, R.string.net_state, Toast.LENGTH_LONG).show();
//            finish();
//            return;
//        }
        versionCode=Integer.toString(getVersionCode(getApplication()));//获取版本号
        PreferenceUtil.putString(BaseActivity.currentActivity, "versionCode", versionCode);//存储版本号
        versionName=getVersionName(getApplication());
        PreferenceUtil.putString(BaseActivity.currentActivity, "versionName", versionName);//存储版本名称

        new Handler().postDelayed(runnable, 2000);



    }


    /**
     * 获取本地Apk版本号
     * @param context 上下文
     * @return int
     */
    public static int getVersionCode(Context context) {
        int verCode = -1;
        try {
            verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        }  catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verCode;
    }

    /**
     * 获取本地Apk版本名
     * @param context 上下文
     * @return int
     */
    public static String getVersionName(Context context) {
        String name=null;
        try {
            name= context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        }  catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return name;
    }


    private Runnable runnable = new Runnable() {
        public void run() {
            if (isLogin){
                if(userType.equals("0")){
                    startActivityAndFinish(MainActivity.class);
                }else{
                    startActivityAndFinish(CareMainActivity.class);
                }

            }
            else {
                startActivityAndFinish(LoginActivity.class);
            }

        }
    };

}
